﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using JoltEnvironment.Sql;

namespace ClusterServer
{
    public class ClusterManager
    {
        #region Fields

        /// <summary>
        /// Id of the cluster.
        /// </summary>
        public int ID;

        /// <summary>
        /// Name of the cluster.
        /// </summary>
        public string Name;

        /// <summary>
        /// IP address of the cluster.
        /// </summary>
        public string IP;

        /// <summary>
        /// Is cluster enabled?
        /// </summary>
        public int Enabled;

        #endregion

        #region Construct

        /// <summary>
        /// Constructs a new cluster manager.
        /// </summary>
        public ClusterManager()
        {
            this.ID = int.Parse(Program.Config.Get("Cluster.Id", "1"));
            this.Name = Program.Config.Get("Cluster.Name", "Cluster");
            this.IP = Program.Config.Get("Cluster.Ip", "127.0.0.1");
            this.Enabled = int.Parse(Program.Config.Get("Cluster.Enabled", "1"));
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds the server to the MySQL server list.
        /// </summary>
        public void Add()
        {
            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Client.AddParameter("id", this.ID);
                Client.AddParameter("name", this.Name);
                Client.AddParameter("ip", this.IP);
                Client.AddParameter("enabled", this.Enabled);

                Client.ExecuteQuery("DELETE FROM server_tbl WHERE id=@id;");
                Client.ExecuteQuery("INSERT INTO server_tbl VALUES(-1, @id, @name, @ip, @enabled, 0);");
            }
        }

        /// <summary>
        /// Removes the server from the MySQL server list.
        /// </summary>
        public void Remove()
        {
            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Client.AddParameter("id", this.ID);

                Client.ExecuteQuery("DELETE FROM server_tbl WHERE id=@id;");
            }
        }

        #endregion
    }
}
